import 'dart:io';

import 'package:app/constants/theme_data.dart';
import 'package:app/page/container/home/page_container_index.dart';
import 'package:app/page/container/home/page_container_user_center.dart';
import 'package:app/page/container/home/page_home_view.dart';
import 'package:app/utils/user_utils.dart';
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  UserStates().loadData();
  runApp(const MyApp());
  doWhenWindowReady(() {
    //const initialSize = Size(1280, 720);
    appWindow.minSize = const Size(405, 720);
    appWindow.maxSize = const Size(1920, 1080);
    //appWindow.size = initialSize;
    appWindow.alignment = Alignment.center;
    appWindow.title = "云间客";
    appWindow.show();
  });
}

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return MainPageState();
  }
}

class MainPageState extends State<MainPage> {
  final List<Widget> _pages = const [PageContainer(), UserCenterView()];
  int _currentIndex = 0;
  late PageController _controller;
  @override
  void initState() {
    super.initState();
    _controller = PageController(initialPage: _currentIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
        //禁止滑动
        physics: const NeverScrollableScrollPhysics(),
        controller: _controller,
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          _controller.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Image.asset(
              "assets/images/home.png",
              width: 50,
              height: 50,
            ),
            activeIcon: Image.asset(
              "assets/images/home_h.png",
              width: 50,
              height: 50,
            ),
            label: "首页",
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              "assets/images/me.png",
              width: 50,
              height: 50,
            ),
            activeIcon: Image.asset(
              "assets/images/me_h.png",
              width: 50,
              height: 50,
            ),
            label: "我的",
          ),
        ],
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  Widget getBodyView() {
    Widget view = const PageHomeView();
    Widget? windowsTitleBar;
    if (!kIsWeb &&
        (Platform.isWindows ||
            Platform.isLinux ||
            Platform.isMacOS ||
            Platform.isFuchsia)) {
      windowsTitleBar = WindowTitleBarBox(
        child: Row(
          children: [
            Expanded(
                child: MoveWindow(
              child: const SizedBox(
                  width: 60,
                  height: 60,
                  child: Center(
                      child: Text(
                    "云间客",
                    style: TextStyle(
                        fontWeight: FontWeight.normal,
                        fontSize: 14,
                        fontStyle: FontStyle.normal,
                        decoration: TextDecoration.none,
                        color: Colors.black),
                  ))),
            )),
            MinimizeWindowButton(),
            MaximizeWindowButton(),
            CloseWindowButton()
          ],
        ),
      );
    }
    if (windowsTitleBar != null) {
      return Column(
        children: [
          windowsTitleBar,
          Expanded(
            child: view,
          )
        ],
      );
    } else {
      return view;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      builder: BotToastInit(),
      navigatorObservers: [BotToastNavigatorObserver()],
      theme:
          ThemeData(primarySwatch: Colors.blue, backgroundColor: Colors.white),
      home: WindowBorder(
        color: Colors.black,
        child: Container(
          decoration: GThemeData.defaultBoxDecoration,
          child: Stack(children: [
            getBodyView(),
          ]),
        ),
      ),
    );
  }
}
